


import java.io.IOException;
import java.util.Random;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;

public class FlipFlop extends GameCanvas implements Runnable {

 private static final int SLEEP_INCREMENT = 10;
     private static final int SLEEP_INITIAL = 150;
     private static final int SLEEP_MAX = 300;

     private TiledLayer tlBase;
     private LayerManager lm;


     private Image img;
     private FlipFlopDesign graphics;
     private Random random;
     private int sleepTime = SLEEP_INITIAL;
     private volatile Thread thread;

     private Form form;

     public FlipFlop(){
          super( true );
     }

     // Когда canvas переходит в режим "невидимый", удаляем thread

     protected void hideNotify(){
          thread = null;
     }

     // Игровой цикл.

     public void run(){
        try {
            this.tlBase = this.graphics.getBackGround();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        this.lm = new LayerManager();
        try {
            graphics.updateLayerManagerForForest(lm);
        } catch (IOException ex) {
            ex.printStackTrace();
        }


         Graphics g = getGraphics();
         g.drawLine(34, UP, UP, UP);

         this.lm.paint(g, 0, 0);
         flushGraphics(0, 0, this.getWidth(), this.getHeight());
    }

     // Когда canvas переходит в видимый режим,
     // создаем поток thread и запускаем игровой цикл.

     protected void showNotify(){
          random = new Random();

          thread = new Thread( this );
          thread.start();
     }
}